;
(function() {
  'use strict';
  angular.module('controllers.accessSystem.packetLoss', [])
    .controller('packetLossCtrl', packetLossCtrl);

  function packetLossCtrl($scope, $filter, CoreService, CoreLoading, packetLossService, $mdDialog, COREDATA) {
    $scope.flag = {
      state: true,
      arrow: true,
      show: ''
    };
    var time = new Date();
    var year = time.getFullYear();
    var month = time.getMonth();
    var day = time.getDate();
    $scope.sendData = {
      startTime: new Date(year, month, day - 1),
      endTime: new Date(year, month, day - 1),
      business: '',
      lostScene: '',
      serialNum: ''
    };
    $scope.send = {
      page: {
        currentPage: '1',
        pageSize: '10'
      }
    };
    $scope.pageItems = ['10', '20', '40'];

    $scope.initQuery = function() {
      //业务渠道
      packetLossService.businessQuery(function(res) {
        if (res.state === '10000') {
          $scope.businessCodes = res.data;
          $scope.sendData.business = $scope.businessCodes[1].businessCode;
        }
      });
      //丢失场景
      packetLossService.lostSceneQuery(function(res) {
        if (res.state === '10000') {
          $scope.lostSceneCodes = res.data;
          $scope.sendData.lostScene = $scope.lostSceneCodes[0].lostSceneCode;
        }
      });
    };
    //导出路劲
    $scope.exportUrl = COREDATA.FORMPREFIX + 'uniformPay/lostMessage/export';
    $scope.search = function(isPage) {
      if (!$scope.flag.state && !isPage) {
        $scope.flag.state = true;
        return;
      }
      CoreLoading.setState(true);
      $scope.title = packetLossService.sendTitle($scope.sendData, $scope.businessCodes, $scope.lostSceneCodes);
      var send = packetLossService.sendData($scope.sendData);
      if ($scope.flag.state) {
        send.currentPage = '1';
      } else {
        send.currentPage = $scope.send.page.currentPage;
      }
      send.pageSize = $scope.send.page.pageSize;
      //导出绑定
      $scope.exportParam = send;
      packetLossService.packetLossQuery(send, function(res) {
        if (res.state === '10000' && res.data.length >= 1) {
          $scope.items = res.data;
          $scope.send.page.currentPage = res.currentPage;
          $scope.send.page.pageSize = res.pageSize + '';
          $scope.send.page.totalCount = res.totalCount;
          $scope.flag.state = false;
          $scope.flag.arrow = false;
          $scope.flag.show = 'table';
        } else if (res.data.length === 0) {
          CoreService.dialog('查询无数据');
          $scope.flag.state = true;
          $scope.flag.arrow = true;
          $scope.flag.show = '';
        } else {
          CoreService.dialog('查询错误');
          $scope.flag.state = true;
          $scope.flag.arrow = true;
          $scope.flag.show = '';
        }
        CoreLoading.setState(false);
      });
    };
    $scope.click = function(item) {
      $mdDialog.show({
        locals: {
          item: item
        },
        templateUrl: 'views/accessSystem/packetLoss/model.html',
        controller: ['$scope', 'item', '$mdDialog', function($scope, item, $mdDialog) {
          if (item.messageDetails.rspCode === undefined) {
            $scope.show = 'showA';
          } else {
            $scope.show = 'showBC';
          }
          $scope.items = item.messageDetails;
          $scope.hide = function() {
            $mdDialog.hide();
          };
        }]
      });
    };
  }

  packetLossCtrl.$inject = ['$scope', '$filter', 'CoreService', 'CoreLoading', 'packetLossService', '$mdDialog', 'COREDATA'];
})();